package com.uyayo.system.config.service.impl;

import java.util.ArrayList;
import java.util.List;


import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.system.config.model.SysIconCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import com.uyayo.system.config.entity.SysIcon;
import com.uyayo.system.config.mapper.SysIconMapper;
import com.uyayo.system.config.service.SysIconService;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;


@Service("iconService")
public class SysIconServiceImpl implements SysIconService {

	@Autowired
	private SysIconMapper sysIconMapper;
		
	@Override
	public SysIcon getIconById(Integer id){
		return sysIconMapper.selectByPrimaryKey(id);
	}



	@Override
	public PageBean<SysIcon> selectIconByPager(Integer pageCurrent, Integer pageSize, String sort, String order,SysIconCondition sysIconCondition) {

		List<SysIcon> sysIcons;
		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("type", "type"));
		orderBies.add(new OrderBy("code", "code"));
		orderBies.add(new OrderBy("clazz", "clazz"));
		orderBies.add(new OrderBy("seq", "seq"));
		orderBies.add(new OrderBy("description", "description"));
		orderBies.add(new OrderBy("createdTime", "created_time"));
		orderBies.add(new OrderBy("modifiedTime", "modified_time"));
		orderBies.add(new OrderBy("remark", "remark"));
		OrderByUtil orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);
		PageHelper.startPage(pageCurrent, pageSize);
		Example example = new Example(SysIcon.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Example.Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(sysIconCondition.getClazz())){
			criteria.andLike("clazz","%"+sysIconCondition.getClazz()+"%");
		}
		if(!StringUtils.isEmpty(sysIconCondition.getDescription())){
			criteria.andLike("description","%"+sysIconCondition.getDescription()+"%");
		}

		if(sysIconCondition.getBeginCreatedTime()!=null || sysIconCondition.getEndCreatedTime()!=null){
			criteria.andBetween("createdTime", sysIconCondition.getBeginCreatedTime(), sysIconCondition.getEndCreatedTime());
		}
		sysIcons = sysIconMapper.selectByExample(example);

		//取记录总条数
		PageInfo<SysIcon> pageInfo = new PageInfo<>(sysIcons);

		return new PageBean<>(pageInfo.getTotal(), pageCurrent, pageSize, sysIcons);
	}

	@Override
	public int saveIcon(SysIcon sysIcon) {
		return sysIconMapper.insertSelective(sysIcon);
	}
	
	@Override
	public int updateIcon(SysIcon icon) {
		return sysIconMapper.updateByPrimaryKeySelective(icon);
	}

	@Override
	public int deleteIcon(Integer id) {
		return sysIconMapper.deleteByPrimaryKey(id);
	}


}
